package com.shop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.shop.entity.Comment;
import com.shop.entity.Product;
import com.shop.util.HibernateUtil;

public class ProductDaoImpl {

	// 判断商品是否存在
	public boolean isExist(Product product) {
		Session session = null;
		session = HibernateUtil.openSession();
		String hql = "from Product where name =  ? ";
		Query query = session.createQuery(hql).setProperties(product);
		query.setParameter(0, product.getName());
		List<Product> list = query.list();
		product = null;
		if (list.size() > 0) {
			product = list.get(0);
			return true;
		} else {
			return false;
		}

	}

	// 查询所有的商品
	public List<Product> getAllProduct() {
		Session session = null;
		session = HibernateUtil.openSession();
		String hql = "from Product";
		Query query = session.createQuery(hql);
		return query.list();
	}

	// 增加商品
	public void addProduct(Product product) {
		Session session = null;
		session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		session.save(product);
		tx.commit();
		session.close();
	}

	// 商品搜索
	public List<Product> getProductsByWord(String searchWord) {
		Session session = null;
		session = HibernateUtil.openSession();
		String queryWord = "%";
		for (int i = 0; i < searchWord.length(); i++) {
			queryWord += String.valueOf(searchWord.charAt(i)) + "%";
		}
		// System.out.println("我搜索了"+queryWord);
		String hql = "from Product where name like ?";
		Query query = session.createQuery(hql);
		query.setParameter(0, queryWord);
		return query.list();
	}

	// 删除商品通过id
	public void deleteProduct(int id) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.openSession();
			tran = session.beginTransaction();
			Product p = session.get(Product.class, id);
			session.delete(p);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			if (null != session) {
				session.close();
			}
		}
	}

	// 修改商品信息
	public boolean updateProduct(Product product) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.openSession();
			tran = session.beginTransaction();
			Query query = session.createQuery("update Product set name=?,description=?, price=?  where id=?");
			query.setParameter(0, product.getName());
			query.setParameter(1, product.getDescription());
			query.setParameter(2, product.getPrice());
			query.setParameter(3, product.getId());
			return query.executeUpdate() > 0;

		} catch (Exception e) {
			e.printStackTrace();
			// tran.rollback();
		} finally {
			if (null != session) {
				session.close();
			}
		}
		return false;
	}

	// 查找商品 by id
	public Product findProductById(int id) {
		Session session = null;
		session = HibernateUtil.openSession();
		String hql = "from Product where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(0, id);
		return (Product) query.uniqueResult();
	}
}
